package com.wyq.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wyq.bean.Audience;
import com.wyq.service.AudienceService;

public class AudienceManagerAction extends ActionSupport {
	private AudienceService audienceService;
	private String selIDStr;
	
	private String showInfoId;//展会ID
	private String start;
	private String limit;

	public void setAudienceService(AudienceService audienceService) {
		this.audienceService = audienceService;
	}

	public void setSelIDStr(String selIDStr) {
		this.selIDStr = selIDStr;
	}
	
	//获取注册用户信息
	public String findAudience(){
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			response.setContentType("application/json; charset=utf-8");
			PrintWriter out=response.getWriter();
			
			//System.out.println("limit:" + limit + ":start:" + start);
			List list = audienceService.findAllAudiences(showInfoId);
			List audienceList = audienceService.findPageAudiences(start,limit,showInfoId);
			String jsonStr = "{totalcount:" + list.size() + ",audienceList:" + JSONArray.fromObject(audienceList).toString() + "}"; 
			out.print(jsonStr);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	}
	
	//删除用户
	public String deleteAudience() throws Exception {
		ActionContext context = ActionContext.getContext();   
		HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		int id = -1;
		int successFlag = 0;
		int errorFlag = 0;
		String[] selIDArr = null;
		if(selIDStr != null){
			selIDArr = selIDStr.split(",");
		}
		for(int i=0;i<selIDArr.length;i++){
			if(selIDArr[i] != null){
				try{
					id = Integer.parseInt(selIDArr[i]);
					Audience audience = new Audience();
					audience.setId(id);
					audienceService.removeAudience(audience);
					successFlag++;
				}catch(Exception e){
					e.printStackTrace();
					errorFlag++;
					continue;
				}
			}
		}
		out.print("{success:true,data:'成功删除"+successFlag+"条记录，失败"+errorFlag+"条'}");
		return null;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public void setShowInfoId(String showInfoId) {
		this.showInfoId = showInfoId;
	}
	
	

}
